
/***************** Modification History ***********************************
   @doc
   @module Clerk.c - Clerk functionality |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Clerk/Clerk.c $ 
 ***************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>

#ifdef  CLERK_DEFINED

//#include <beetle.h>
//#include <beetleplug.h>

#include <passwd.h>

#include <applidl.h>
#include <aclconio.h>
#include <acldev.h>
#include <aclstr.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <actutil.h>

#ifdef DEBUG_VSCLERK
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <table.h>
#include <clerk.h>
#include <clrkrpts.h>
#include <funcids.h>
#include <ui.h>
#include <list.h>     /* pj1 */
#include <init.h>
#include <msg.h>
#include <msgclrk.h>
#include <msgfuncs.h>
#include <msgclrk.h>
#include <oper.h>
#include <cst.h>
#include <cdt.h>
#include <tct.h>
#include <udt.h>
#include <tblutil.h>
#include <trt.h>
#include <tran.h>
#include <amount.h>
#include <spdebug.h>
#include <passwd.h>     
#include <entry.h>
#include <format.h>
#include <review.h>   
#include <mdlfuncs.h>
#include <pfr.h>
#include <codesec.h>
#include <gds.h>
#include <menufunc.h>
#include <menus.h>
#include <tblutil.h>


#define CLERK_C

#define lnSelectAppTypePrompt(lnMsgId1, lnMsgId2)       (inGetApplicationType() == RETAIL ? lnMsgId1 : lnMsgId2)

FUNCTION_TABLE srClerkFuncs[] = {
    {inAddClerkOperation,         ADD_CLERK},
    {inDelClerkOperation,         DEL_CLERK},
    {inModClerkOperation,         MOD_CLERK},
    {inClearClerkOperation,       CLEAR_CLERK},
    {inClerkTableReportOperation, PRINT_CLERK_TABLE_REPORT},
    #ifdef BATCH_DEFINED
    {inClerkDetailReportOperation,PRINT_CLERK_DETAIL_REPORT},
    {inClearClerkOperation,       CLEAR_CLERK},
    {inClerkTotalsReportOperation,PRINT_CLERK_TOTALS_REPORT},
    {inDiscountTipReportOperation,PRINT_DISCOUNT_TIP_REPORT},
    {inIRSTipReportOperation,     PRINT_IRS_TIP_REPORT},
    {inUnadjustedOperation,       PRINT_UNADJUSTED_REPORT},
    #endif
    {inLogInOperation,            LOGIN_CLERK},
    {inLogOffOperation,           LOGOFF_CLERK},
    {inUpdateClerkFlow,           UPDATE_CLERK},
    {inGetClerkId,                GET_CLERK_ID},
    {0,                           END_OF_FUNC_TABLE},

};

/********************************************************************************************* 
* @func int | inAddClerkFunctions | 
*       Adds CLERK functions to the flexi record.
*
* @rdesc return values.
* @flag int    |  VS_ERR, VS_ESCAPE or VS_SUCCESS
* @end
**********************************************************************************************/

int inAddClerkFunctions(void) {
    int inCount = 0;

    /* Add CLERK functions to the flexi record */   
    do {
        if (inAddFunctionPointer(srClerkFuncs[inCount].inFieldID, srClerkFuncs[inCount].pinFunction) == VS_ERR)
            return (VS_ERR);
        inCount++;
    } while (srClerkFuncs[inCount].inFieldID != END_OF_FUNC_TABLE);

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | vdDispTitleStrAtRight |
*    
*   Displays the message parameter on top right of screen
*    Input Parameters :  inMsgNum= the message number from the selectedmsg file
*                        inClearDp= the type of clearing action to be performed  
* 
* @rdesc return values.
* @flag int    |  no return value
* @end
**********************************************************************************************/

void vdDispTitleStrAtRight(long lnMsgNum,int inClearDp) {
    int inXCord,
    x1,
    x2,
    y1,
    y2;
    char szDisplayBuffer[DISP_STR_LEN];
    int inRowLim = inGetRowLim();

    /** /pdebug(("--vdDispTitleStrAtRight--"));*/

    wherewin(&x1, &y1, &x2, &y2);
    *szDisplayBuffer = NULL_CH;
    vdGetMessageFromFile(lnMsgNum, szDisplayBuffer);
    if (*szDisplayBuffer == NULL_CH)
        return;
    window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inTitleLine);
    clrscr();
    inXCord = obDisp.inCharsPerRow - strlen(szDisplayBuffer) + 1;
    vdDisplayAt(inXCord, obDisp.inTitleLine, szDisplayBuffer, inClearDp);
    window(x1, y1, x2, y2);
}

/********************************************************************************************* 
* @func int | inValidateClerk |
*    
* Verify that clerk number is valid 
* 
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
* @end
**********************************************************************************************/

int inValidateClerk(char* pszClerkID) {
    char szPwd[PASSWORD_SIZE + 1];
    int inResult;

    if ((inResult = inGetUDTPwd(pszClerkID, szPwd)) < VS_SUCCESS)
        vdSGErrorMessage(lnSelectAppTypePrompt(INV_CLERK_ERR_MSG21, INV_SERVER_ERR_MSG21));
    return(inResult);
}

/********************************************************************************************* 
* @func int | inGetClerkID |
*    
*  Validate the Clerk id entered by the user.
   and verify user is defined in UDT.
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, VS_SUCCESS
* @end
**********************************************************************************************/

int inGetClerkId(TRANSACTION_OBJECT* pobTran) {
    char szClerkID[CLERK_ID_SIZE + 1];
    int inResult = VS_SUCCESS;
    VS_BOOL fScrollDisplay = !(obDisp.blNoScrollEntries);

    pdebug(("--inGetClerkId--"));

    int2str(szClerkID, 0);
    strlcpy(pobTran->srBKey.szClerkId, szClerkID, CLERK_ID_SIZE);
    if (inGetClerkMode() == CLERK_PROMPT) {
        if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), fScrollDisplay, szClerkID, INPUT_ALNUMS, CLERK_ID_SIZE, inValidateClerk)) >= 0) {
            strlcpy(pobTran->srBKey.szClerkId, szClerkID, CLERK_ID_SIZE);
            return(VS_SUCCESS);
        }
    }

    /* Checks if clerk is logged on             */
    if (inGetClerkMode() == CLERK_LGN)
        while ((inResult = inCheckLogOn(pobTran)) != VS_SUCCESS) {
            vdClearStatusMessage();

            if (inResult == VS_ESCAPE)
                return(inResult);  /* fix for invalid password bug */

            else if (act_kbd_pending_test(KEY_CANCEL) == 1)
                return(VS_ESCAPE);
        }

    return(inResult);
}

/********************************************************************************************* 
* @func int | inGetClerkServerId |
*    
*  Validates the Clerk id entered by the user.
* 
* @rdesc return values.
* @flag int    |  VS_SUCCESS
* @end
**********************************************************************************************/

int inGetClerkServerId(TRANSACTION_OBJECT* pobTran) {
    char szClerkID[CLERK_ID_SIZE + 1];
    int inResult = VS_SUCCESS;

    pdebug(("--inGetClerkServerId--"));

    if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), SCROLL_DISPLAY_TRUE, szClerkID, INPUT_ALNUMS, CLERK_ID_SIZE, inValidateClerk)) >= 0) {
        strlcpy(pobTran->srBKey.szClerkId, szClerkID, CLERK_ID_SIZE);
        return(VS_SUCCESS);
    }
    return(inResult);
}

static char ifOddBoundary(unsigned char* buff) {
    return (char) (((long) buff) % 2);
}

/********************************************************************************************* 
* @func int | pAddClerk |
*    
*  Add validated clerk to the User Definition Table
* 
* @rdesc return values.
* @flag int    |  VS_SUCCESS
* @end
**********************************************************************************************/

int pAddClerk(TRANSACTION_OBJECT* pobTran) {
    int inResult;
    char szSaveClerkID[CLERK_ID_SIZE + 1];
    char szClerk[CLERK_ID_SIZE + 1];
    char szPassword[PASSWORD_SIZE + 1];
    char szPasswordVerify[PASSWORD_SIZE + 1];
    char szManPassword[PASSWORD_SIZE + 1];
    unsigned char ucUDTRecBuff[SIZE_UDT_REC + 1];
    unsigned char * ucUDTRec = ucUDTRecBuff;

    if (ifOddBoundary(ucUDTRec))   /* if on odd boundary increment pointer */
        ucUDTRec++;     
    if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), SCROLL_DISPLAY_FALSE, szClerk, INPUT_ALNUMS, CLERK_ID_SIZE, inValidateString)) <= VS_ERR)
        return(inResult);

    if ((inResult = inCheckUDTClerkId(szClerk)) != VS_ERR)
        if (inResult == VS_SUCCESS)  /* Found matching clerk */ {
            vdDisplayMessageFromFile(lnSelectAppTypePrompt(CLERK_ALREADY_ERR_MSG21, SERVER_ALREADY_ERR_MSG21), ERROR_MSG);
            return(VS_ERR);
        }

    if ((inResult = inGetPassword(PASSWD_MSG15, szPassword)) <= VS_ERR)
        return(inResult);


    while ((inResult = inCheckUDTPwd(szPassword, szSaveClerkID)) == VS_SUCCESS) {
        vdDisplayMessageFromFile(PASSWORD_ALREADY_ERR_MSG, ERROR_MSG);
        vdClearStatusMessage();

        if ((inResult = inGetPassword(PASSWD_MSG15, szPassword)) <= VS_ERR)
            return(inResult);
    }

    if (inResult == VS_ESCAPE)
        return(inResult);

    strcpy(szManPassword, szGetSuperPwd());        /* check supervisor level security */
    if (strcmp(szManPassword, szPassword) == 0) {
        vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, ERROR_MSG);
        return(VS_ERR);
    }

    strcpy(szManPassword, szGetManagerPwd());      /* check manager level security */

    if (strcmp(szManPassword, szPassword) == 0) {
        vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, ERROR_MSG);
        return(VS_ERR);
    }

    if ((inResult = inGetPassword(REENTER_PWD_MSG, szPasswordVerify)) != VS_ERR)
        if (inResult == VS_SUCCESS) {
            vdDisplayMessageFromFile(PASSWORD_ALREADY_ERR_MSG, ERROR_MSG);
            return(VS_ERR);
        }
    if (strcmp(szPassword, szPasswordVerify) != 0) {
        vdDisplayMessageFromFile(PWD_MISMATCH_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    vdSetLogedInFromStruct(0, ucUDTRec);
    vdSetCashTipFromStruct(0L, ucUDTRec);

    if ((inResult = inInsertUDTEntry(szClerk, szPassword, ucUDTRec)) != VS_SUCCESS) {
        vdSGErrorMessage(UDT_ERR_MSG21);
        return(inResult);
    }

    vdDisplayMessageFromFile(lnSelectAppTypePrompt(CLERK_ADDED_MSG21, SERVER_ADDED_MSG21), STATUS_MSG);
    pause(50);
    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | pDelClerk |
*    
*  Deletes the active clerk from the User Definition Table
* 
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
* @end
**********************************************************************************************/

int pDelClerk() {
    int inResult = VS_SUCCESS;
    char szClerk[CLERK_ID_SIZE + 1];
    char szPassword[PASSWORD_SIZE + 1];


    if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), SCROLL_DISPLAY_FALSE, szClerk, INPUT_ALNUMS, CLERK_ID_SIZE, inValidateClerk)) > VS_SUCCESS) {
        if ((inGetPassword(PASSWD_MSG15, szPassword)) > VS_SUCCESS) {
            if ((inResult = inDeleteUDTEntry(szClerk, szPassword)) == VS_SUCCESS) {
                vdDisplayMessageFromFile(lnSelectAppTypePrompt(CLERK_DELETED_MSG21, SERVER_DELETED_MSG21), STATUS_MSG);
                pause(50);
            } else if (inResult == VS_ERR)
                vdDisplayMessageFromFile(lnSelectAppTypePrompt(INV_CLERK_ERR_MSG21, INV_SERVER_ERR_MSG21), ERROR_MSG);
        }
    }

    return(inResult);
}

/********************************************************************************************* 
* @func int | pModClerk |
*    
*  Modify the selected clerk record
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, VS_SUCCESS
* @end
**********************************************************************************************/

int pModClerk(void) {
    signed int inResult,
    inSelect;
    char szClerkID[CLERK_ID_SIZE + 1];
    char szSaveClerkID[CLERK_ID_SIZE + 1];
    char szOldPassword[PASSWORD_SIZE + 1];
    char szNewPassword[PASSWORD_SIZE + 1];
    char szPasswordVerify[PASSWORD_SIZE + 1];
    char szManPassword[PASSWORD_SIZE + 1];
    char szDisplayBuffer[DISP_STR_LEN + 1];
    BOOL fQuitLoop = FALSE;
    char* chNull = 0;

    if (inGetPWSecurityLevel() == ACCESS_WITH_CLERK_PASSWORD)
        strcpy(szClerkID, srGDS.szClerkIdFromPW);
    else {
        if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), SCROLL_DISPLAY_FALSE, szClerkID, INPUT_ALNUMS, CLERK_ID_SIZE, inValidateClerk)) <= VS_ERR)
            return(inResult);
    }

    vdClearNonTitleLines();  /* bb1 */

    if (fGetCashTipOn())  /* bb1 DFW 163 */ {
        inSelect = obDisp.inDO_ChooseOne(chNull, chNull, pchGetMsg(MOD_CLERK_OPTION_MSG5x), 2, NULL_PFI); /* bb1 */

        if (inSelect == VS_ESCAPE)
            return(VS_ESCAPE);

        /* Modify cash tip */
        if (inSelect == 2) {
            if ((inResult = inAdjustCashTip(szClerkID)) == VS_SUCCESS) {
                vdDisplayMessageFromFile(lnSelectAppTypePrompt(CLERK_MODIFIED_MSG21, SERVER_MODIFIED_MSG21), STATUS_MSG);
                pause(50);
            }
            clrscr();

            vdGetMessageFromFile(MOD_SERVER_TITLE_MSG21, szDisplayBuffer);

            vdDisplayAt(1, obDisp.inTitleLine, szDisplayBuffer, CLR_EOL);           


            return(VS_SUCCESS);
        }
    }       

    /* Modify password */
    do {
        if ((inResult = inGetPassword(OLD_PWD_MSG, szOldPassword)) <= VS_ERR)
            return (inResult);

        if ((inResult = inVerifyClerkPwd(szOldPassword, szClerkID)) != VS_SUCCESS) {
            vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, WARNING_MSG);
            vdClearStatusMessage();
        } else
            fQuitLoop = TRUE;
    } while (fQuitLoop != TRUE);  /* bb1 added loop */

    if ((inResult = inGetPassword(NEW_PWD_MSG, szNewPassword)) <= VS_ERR)
        return(inResult);

    if ((inResult = inCheckUDTPwd(szNewPassword, szSaveClerkID)) == VS_SUCCESS) {
        vdDisplayMessageFromFile(PASSWORD_ALREADY_ERR_MSG, WARNING_MSG);
        vdClearStatusMessage();
        return(VS_SUCCESS); /*  returns success to stayed in the loop */
    }

    strcpy(szManPassword, szGetSuperPwd());       /* check supervisor level security */
    if (strcmp(szManPassword, szNewPassword) == 0) {
        vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, WARNING_MSG);
        vdClearStatusMessage();
        return(VS_SUCCESS);
    }

    strcpy(szManPassword, szGetManagerPwd());  /* check manager level security */

    if (strcmp(szManPassword, szNewPassword) == 0) {
        vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, WARNING_MSG);
        vdClearStatusMessage();        
        return(VS_SUCCESS);
    }

    if ((inResult = inGetPassword(REENTER_PWD_MSG, szPasswordVerify)) <= VS_ERR)
        return(inResult);

    if ((inResult = strcmp(szNewPassword, szPasswordVerify)) != 0) {
        vdDisplayMessageFromFile(PWD_MISMATCH_ERR_MSG, WARNING_MSG);
        vdClearStatusMessage();
        return(VS_SUCCESS); /*  returns success to stayed in the loop */
    }
    inModifyPassword(szClerkID, szNewPassword);

    vdDisplayMessageFromFile(lnSelectAppTypePrompt(CLERK_MODIFIED_MSG21, SERVER_MODIFIED_MSG21), STATUS_MSG);
    pause(50);
    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inAdjustCashTip |
*    
*  Get the new cash tip amount 
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, VS_SUCCESS, VS_ERR
* @end
**********************************************************************************************/

int inAdjustCashTip(char* szClerkID) {
    char szAmount[AMT_SIZE + 1];
    int inResult;
    char szAmt[AMT_SIZE + 1];
    char* szDisplaySymbol;
    char szDisplayBuffer[DISP_STR_LEN + 1];
    long lnOldCashTipAmt;
    unsigned char ucUDTRecBuff[SIZE_UDT_REC + 1];
    unsigned char * ucUDTRec = ucUDTRecBuff;
    FHANDLE hUDTFHandle;

    pdebug(("--inAdjustCashTip--"));

    if (ifOddBoundary(ucUDTRec))   /* if on odd boundary increment pointer */
        ucUDTRec++;     
    memset(szDisplayBuffer, '\0', sizeof(szDisplayBuffer));
    szAmount[0] = '\0';
    szAmt[0] = '\0';
    szDisplaySymbol = szGetCurSymbol();

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) < 0)
        return(VS_ERR);

    if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) != (long) GEN_VER_SIZE) {
        close(hUDTFHandle);
        return(VS_ERR);
    }
    do {
        inResult = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
        if (inResult == 0)
            break;                
        if (inResult != (int) SIZE_UDT_REC)   /* Read error, get out */ {
            close(hUDTFHandle);
            return(VS_ERR);
        }

        if (strcmp(szClerkID, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
            lnOldCashTipAmt = lnGetCashTipFromStruct(ucUDTRec);
            sprintf(szAmt, "%ld", lnOldCashTipAmt); 
            break;
        }
    } while (1);

    close(hUDTFHandle);

    vdFormatAmount(szAmt, VS_FALSE);
    vdTrimLeadSpaces(szAmt);

    vdGetMessageFromFile(CASH_TIP_AMT_MSG, szDisplayBuffer);

    strcat(szDisplayBuffer, " ");

    vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine, szDisplayBuffer, CLR_EOL);
    vdDispStrAtRight(szAmt, obDisp.inDetailLine, NO_CLEAR);

    vdGetMessageFromFile(NEWAMT_MSG11, szDisplayBuffer);
    vdDisplayAt(obDisp.inHomeCol, obDisp.inInputLine, szDisplayBuffer, CLR_EOL);

    // BT_TRACE ('I', szDisplayBuffer);
    if ((inResult = inGetAmount(szAmount, VS_TRUE, 0L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
        return(inResult);

    if (inResult >= 0)
        inUpdateUDTRecord(szClerkID, atol(szAmount));

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inCheckLogOn |
*    
*  This functions checks whether the user logged in to the
*  system has entered correct password or not. The name and
*  password are in  multeventBuf[0] and multeventBuf[1]
* 
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
* @end
**********************************************************************************************/

int inCheckLogOn(TRANSACTION_OBJECT* pobTran) {
    char szClerk[CLERK_ID_SIZE + 1];
    int inResult = VS_ERR;
    VS_BOOL fScrollDisplay = !(obDisp.blNoScrollEntries);

    if ((inGetClerkMode() == NO_CLERK))
        return(VS_SUCCESS);

    if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), fScrollDisplay, szClerk, INPUT_ALNUMS, CLERK_ID_SIZE, NULL_PFI)) <= VS_ERR)
        return(inResult);

    while ((inResult = inGetUDTLogedIn(pobTran, szClerk)) != VS_ESCAPE) {
        if (inResult == VS_SUCCESS) {
            strlcpy(pobTran->srBKey.szClerkId, szClerk, CLERK_ID_SIZE);
            return(inResult);
        } else {
            vdDisplayMessageFromFile(lnSelectAppTypePrompt(INV_CLERK_ERR_MSG21, INV_SERVER_ERR_MSG21), ERROR_MSG);
            vdClearStatusMessage();
        }

        /* Get out in Clear key press */
        if (act_kbd_pending_test(KEY_CANCEL))
            return (VS_ESCAPE);

        if ((inResult = inEnterWithMessage(lnSelectAppTypePrompt(CLERK_ID_MSG, SERVER_ID_MSG), SCROLL_DISPLAY_FALSE, szClerk, INPUT_ALNUMS, CLERK_ID_SIZE, NULL_PFI)) <= VS_ERR)
            return(inResult);
    }  /* end while */



    return(inResult);
}

/********************************************************************************************* 
* @func int | inUpdateClerkFlow | 
*       This routine updates the Clerk information if necessary.  It is intended to be used from the TRT.
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS
* @flag int    |  VS_ERR
* @end
**********************************************************************************************/


/* #ifdef BATCH_DEFINED */

int inUpdateClerkFlow(TRANSACTION_OBJECT* pobTran) {
    char szADTFileName[FILENAME_SIZE + 1] ;

    /** /pdebug(("--inUpdateClerkFlow--"));*/

    if (!fGetDataCapture(pobTran))
        return(VS_SUCCESS);

    /* pj1 - skip inUpdateAccum if open tab */  
    if (pobTran->fOTBatchOpen)
        return(VS_SUCCESS);

    /* Set the power fail recovery state */
    inPFRStateStoreAll(PFR_BEGIN_UPDATE_CLERK, pobTran, 1, 0);

    if (inSetUTTAmount(pobTran) != VS_SUCCESS) {
        /* Set the power fail recovery state */
        inPFRStateStoreAll(PFR_UPDATE_CLERK_COMPLETE, pobTran, 1, 0);
        vdSGErrorMessage(CLERK_TOTAL_ERR_MSG21);
        return(VS_ERR);
    }

    vdGetADTFileName(szADTFileName); 

    if (inInsertADTEntry(pobTran->srBKey.szClerkId, szADTFileName) != VS_SUCCESS) {
        /* Set the power fail recovery state */
        inPFRStateStoreAll(PFR_UPDATE_CLERK_COMPLETE, pobTran, 1, 0);
        vdSGErrorMessage(ADT_FILE_ERR_MSG21);
        return(VS_ERR);
    }        

    /* Set the power fail recovery state */
    inPFRStateStoreAll(PFR_UPDATE_CLERK_COMPLETE, pobTran, 1, 0);
    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inClearClerkOperation |
*    
* Clear Clerk totals if no batch records
* 
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
* @end
**********************************************************************************************/

int inClearClerkOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inClearClerkOperation--"));*/

    if (inGetClerkMode() == NO_CLERK) {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

#ifdef BATCH_DEFINED    

    /* Check whether all the batches are empty or not. */
    if (!fChkAllHostBatches(pobTran)) {
        vdDisplayMessageFromFile(BATCH_NOT_EMPTY_ERR_MSG15, ERROR_MSG);
        return(VS_ERR);
    }

    if (inGetApplicationType() == RESTAURANT) {
        if (inResetUDTLogedIn() != VS_SUCCESS) {
            vdDisplayMessageFromFile(SERVER_TOTAL_ERR_MSG21, ERROR_MSG);
            return(VS_ERR);
        }
        vdDisplayMessageFromFile(SERVER_TOTS_DELETED_MSG21, STATUS_MSG);
    } else {
        if (inResetUDTLogedIn() != VS_SUCCESS) {
            vdDisplayMessageFromFile(CLERK_TOTAL_ERR_MSG21, ERROR_MSG);
            return(VS_ERR);
        }
        vdDisplayMessageFromFile(CLERK_TOTS_DELETED_MSG21, STATUS_MSG);
    }
    pause(100);

#endif /* Batch defined */


    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inAddClerkOperation |
*    
* Initiates the add clerk operation
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, BRANCH_EXIT
* @end
**********************************************************************************************/

int inAddClerkOperation(TRANSACTION_OBJECT* pobTran) {
    int inResult;

    /** /pdebug(("--inAddClerkOperation--"));*/
    do {
        inResult = pAddClerk(pobTran);
        vdDisplayMessageFromFile(EMPTY_STR_ERR_MSG, STATUS_MSG);
    } while (inResult != VS_ESCAPE);

    return(BRANCH_EXIT);
}

/********************************************************************************************* 
* @func int | inDelClerkOperation |
*    
* Initiates the delete clerk operation
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, BRANCH_EXIT
* @end
**********************************************************************************************/


int inDelClerkOperation(void) {
    int inResult;

    /** /pdebug(("--inDelClerkOperation--"));*/    
    do {
        inResult = pDelClerk();
        vdDisplayMessageFromFile(EMPTY_STR_ERR_MSG, STATUS_MSG);
    } while (inResult != VS_ESCAPE);

    return(BRANCH_EXIT);
}

/********************************************************************************************* 
* @func int | inModClerkOperation |
*    
* Initiates the modify clerk operation
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, BRANCH_EXIT
* @end
**********************************************************************************************/

int inModClerkOperation(void) {
    int inResult;

    /** /pdebug(("--inModClerkOperation--"));*/    
    do {
        inResult = pModClerk();
        vdDisplayMessageFromFile(EMPTY_STR_ERR_MSG, STATUS_MSG);
    } while (inResult != VS_ESCAPE);

    return(BRANCH_EXIT);
}

/********************************************************************************************* 
* @func int | inLogInOperation |
*    
* Initiates the clerk login operation
* 
* @rdesc return values.
* @flag int    |  VS_ESCAPE, VS_FAILURE, VS_SUCCESS
* @end
**********************************************************************************************/

int inLogInOperation(TRANSACTION_OBJECT* pobTran) {
    int inResult;

    if (inGetClerkMode() == CLERK_LGN) {
        inResult = inCheckLogOn(pobTran);
        while (inResult != VS_SUCCESS) {
            if ((act_kbd_pending_test(KEY_CANCEL) == 1) || (inResult == VS_ERROR) || (inResult == VS_ESCAPE))
                return(VS_ESCAPE);
            vdClearStatusMessage();
            inResult = inCheckLogOn(pobTran);;
        }  

        vdDisplayMessageFromFile(LOGGED_IN_MSG21, STATUS_MSG);
        pause(50); /* so user can read */
    } else {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
        return(VS_FAILURE);
    }

    return(inResult);
}

/********************************************************************************************* 
* @func int | inLogOffOperation |
*    
* Logs off the active clerk
* 
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_FAILURE
* @end
**********************************************************************************************/

int inLogOffOperation(TRANSACTION_OBJECT* pobTran) {
    int inResult;

    if (inGetClerkMode() == CLERK_LGN) {
        inResult = inCheckLogOn(pobTran);

        while (inResult != VS_SUCCESS) {
            if ((act_kbd_pending_test(KEY_CANCEL) == 1) || (inResult == VS_ERROR) || (inResult == VS_ESCAPE))
                vdClearStatusMessage();
            return(VS_ESCAPE);
        }
        vdDisplayMessageFromFile(LOGGED_OFF_MSG, STATUS_MSG);
        pause(50);
    } else {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
        return(VS_FAILURE);
    }
    return(inResult);
}

int CLERK_CODE_CHECK(char* a,char* b,short * c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}

#undef CLERK_C

#endif // CLERK_DEFINED
